//
//  NetListenViewController.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/9/2.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit
import Alamofire
protocol NetListenDelegate: NSObjectProtocol {
    //重新加载数据
    func againReloadTheData()
}
class NetListenViewController: UIViewController {

    weak var delegate: NetListenDelegate?
    var networkConn: Reachability?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        let imageView = UIImageView(frame: CGRect(x: 0, y: DeviceWidth/6, width: DeviceWidth/2, height: DeviceWidth/2))
        imageView.image = UIImage(named: "wangluocuowu")
        imageView.center = CGPoint(x: DeviceWidth/2, y: DeviceHeight/2)
        self.view.addSubview(imageView)
        let line = UILabel.createLineLabelWithFrame(frame: CGRect(x: 0, y: 0, width: DeviceWidth, height: kLineWitdth))
        self.view.addSubview(line)
        let button = UIButton.createButtonWithTitle(title: "重新加载", frame: CGRect(x: 0, y: 0, width: 90, height: 32), target: self, action: #selector(againDownLoadeData), font: 14, textColor: .gray)
        button.center = CGPoint(x: DeviceWidth/2, y: imageView.maxY+40)
        button.addCornerRadius(radius: 3, borderWidth: 0.5, borderColor: .gray)
        self.view.addSubview(button)
        // Do any additional setup after loading the view.
    }
    @objc func againDownLoadeData() {
        let reachability = NetworkReachabilityManager(host: "www.apple.com")
        if(!reachability!.isReachable) {
            UIAlertController.showAlert(message: "请检查您的网络")
            return
        }
        guard self.delegate != nil && (self.delegate?.responds(to: #selector(againDownLoadeData)))! else {
            return
        }
        delegate?.againReloadTheData()
        self.view.removeFromSuperview()
    }
    func showInView(view:UIView) {
        view.addSubview(self.view)
    }
    /*****************************************/
    //展示网络错误的界面
    func goSpecialViewController(tab:UITableView) {
        showInView(view: tab)
        tab.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        tab.isScrollEnabled = false
    }
    func networkStateChangeNSNotificationCenterWithTabelView(table:UITableView) {
        NotificationCenter.default.addObserver(self, selector: #selector(networkStateChange), name: NSNotification.Name.reachabilityChanged, object: nil)
        networkConn = Reachability.forInternetConnection()
        networkConn!.object = table
        networkConn!.startNotifier()
    }
    @objc func networkStateChange() {
        if(!isNetworkEnable()) {
            return
        }
        let obj = networkConn!.object
        guard obj != nil else {
            return
        }
        if(obj is UITableView) {
            let table = obj as! UITableView
            table.isScrollEnabled = true
            table.triggerRefreshing()
        }
    }
    func isNetworkEnable() -> Bool {
        var flag = true
        let r = Reachability(hostName: "www.apple.com")
        let obj = networkConn!.object
        let table = obj as! UITableView
        switch r?.currentReachabilityStatus() {
        case NotReachable:
            flag = false
            goSpecialViewController(tab:table)
            break
        case ReachableViaWWAN:
            self.view.removeFromSuperview()
            table.isScrollEnabled = true
            break
        case ReachableViaWiFi:
            self.view.removeFromSuperview()
            table.isScrollEnabled = true
            break
        default:
            break
        }
        return flag
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
